﻿Imports DevExpress.XtraEditors
Imports DevExpress.XtraSplashScreen

Public Class TiepNhanBenhNhan_DOI
    Dim flagdmbenhnhan As Boolean
    Dim mabenhnhan As String
    Dim indexGrid As Integer
    Private Sub MabenhnhanISearchLookupEdit_AddNewValue(sender As Object, e As DevExpress.XtraEditors.Controls.AddNewValueEventArgs) Handles MabenhnhanISearchLookupEdit.AddNewValue
        DMBenhNhan.Show()
        DMBenhNhan.MdiParent = Me.MdiParent
        DMBenhNhan.WindowState = FormWindowState.Maximized
        Me.flagdmbenhnhan = True
    End Sub
    'Lấy mã bệnh nhân sang MaBenhNhanSearchLookupedit của Tiepnhanbenhnhan
    Private Sub TiepNhanBenhNhan_Activated(sender As Object, e As System.EventArgs) Handles Me.Activated
        If flagdmbenhnhan Then
            Me.DMBenhNhanTableAdapter.Fill(Me.PCV_CLINICDataSet_Don.DMBenhNhan)
            Me.MabenhnhanISearchLookupEdit.EditValue = mabenhnhan
            Me.flagdmbenhnhan = False
        End If
    End Sub
    Private Sub loadDuLieu()
        SplashScreenManager.ShowForm(GetType(WaitForm1))
        Me.DMDichVuTableAdapter.Fill(Me.PCV_CLINICDataSet_Don.DMDichVu)
        Me.DMPhongTableAdapter.Fill(Me.PCV_CLINICDataSet_Don.DMPhong)
        Me.DMBacSiTableAdapter.Fill(Me.PCV_CLINICDataSet_Don.DMBacSi)
        Me.DMKhamBenhTableAdapter.Fill(Me.PCV_CLINICDataSet_Don.DMKhamBenh)
        Me.DMDanTocTableAdapter.Fill(Me.PCV_CLINICDataSet_Don.DMDanToc)
        Me.GioiTinhTableAdapter.Fill(Me.PCV_CLINICDataSet_Don.GioiTinh)
        Me.DMBenhNhanTableAdapter.Fill(Me.PCV_CLINICDataSet_Don.DMBenhNhan)
        Me.BenhNhanTableAdapter.Fill(Me.PCV_CLINICDataSet_Don.BenhNhan)
        Me.TiepNhanBenhNhanDichVuTableAdapter.Fill(Me.PCV_CLINICDataSet.TiepNhanBenhNhanDichVu)
        Me.TiepNhanBenhNhanTableAdapter.Fill(Me.PCV_CLINICDataSet.TiepNhanBenhNhan)
        Me.IGridControlDs01.ForceInitialize()
        Me.IGridViewDs01.MoveLast()
        Me.setHeaderFooter("Công Ty CP Tư Vấn Giải Pháp ERP Phong Cách Việt", "Lâm Văn Bền, P.Tân Kiểng, Q.7, TP.HCM")
        SplashScreenManager.CloseForm()
    End Sub

    Private Sub TiepNhanBenhNhan_DOI_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        Me.loadDuLieu()

    End Sub

    Public Sub New()
        Me.IGridViewPopUp2.setColumnCheck()
        InitializeComponent()
        Me.setSoTabThamChieu(1)
        Me.setSoTab(1)
        Me.setPopUp2(Me.PCV_CLINICDataSet_Don.DMDichVu, Me.DMDichVuBindingSource, Me.DMDichVuTableAdapter)
        Me.setObj(Me.PCV_CLINICDataSet, Me.PCV_CLINICDataSet.TiepNhanBenhNhan, Me.TiepNhanBenhNhanBindingSource, Me.TableAdapterManager, Me.TiepNhanBenhNhanTableAdapter, Me.LoaibenhnhanISearchLookupEdit, Me.TinhtrangISearchLookupEdit)
        Me.setTab1(Me.PCV_CLINICDataSet.TiepNhanBenhNhanDichVu, Me.FKTiepNhanBenhNhanDichVuTiepNhanBenhNhanBindingSource, Me.TiepNhanBenhNhanDichVuTableAdapter)
        Me.setColumnsChangeTab1(New ArrayList From {"soluong", "dongia"})
        Me.setColumnValueTab1(colEx1ChiTiet01, colthanhtien)

    End Sub

    Public Overrides Sub setSubSau_TaoMoi()
        Me.setMacDinhCotAn("ma", "TNBN" & System.Guid.NewGuid().ToString)
        Me.NgaytiepnhanIDateEdit.EditValue = Today()
        Me.LoaibenhnhanISearchLookupEdit.Focus()
        Me.LoaibenhnhanISearchLookupEdit.ShowPopup()
    End Sub

    Private Sub IGridViewChiTiet01_InitNewRow(sender As System.Object, e As DevExpress.XtraGrid.Views.Grid.InitNewRowEventArgs) Handles IGridViewChiTiet01.InitNewRow
        Me.IGridViewChiTiet01.SetFocusedRowCellValue("sotiepnhan", Me.SotiepnhanITextEdit.EditValue)
        Me.IGridViewChiTiet01.SetFocusedRowCellValue("mabenhnhan", Me.MabenhnhanISearchLookupEdit.EditValue)
        Me.IGridViewChiTiet01.SetFocusedRowCellValue("soluong", 0)
        Me.IGridViewChiTiet01.SetFocusedRowCellValue("dongia", 0)
        Me.IGridViewChiTiet01.SetFocusedRowCellValue("thanhtien", 0)
        Me.IGridViewChiTiet01.SetFocusedRowCellValue("uid", System.Guid.NewGuid())
        Me.setFlagInitNewRowTab1(True)
    End Sub

    'Fill DMBenhNhan theo loại bệnh nhân
    Private Sub MabenhnhanISearchLookupEdit_Enter(sender As Object, e As System.EventArgs) Handles MabenhnhanISearchLookupEdit.Enter
        Me.DMBenhNhanTableAdapter.FillLoaiBenhNhan_DMBenhNhan(Me.PCV_CLINICDataSet_Don.DMBenhNhan, Me.LoaibenhnhanISearchLookupEdit.EditValue)
    End Sub

    Private Sub MabenhnhanISearchLookupEdit_EditValueChanging(sender As Object, e As DevExpress.XtraEditors.Controls.ChangingEventArgs) Handles MabenhnhanISearchLookupEdit.EditValueChanging
        Dim editor As SearchLookUpEdit = CType(sender, SearchLookUpEdit)
        Dim rowhandle As Integer = Me.MabenhnhanISearchLookupEdit.Properties.GetIndexByKeyValue(e.NewValue)
        Me.TenITextEdit.EditValue = editor.Properties.View.GetRowCellValue(rowhandle, "ten")
        Me.DantocISearchLookupEdit.EditValue = editor.Properties.View.GetRowCellValue(rowhandle, "dantoc")
        Me.GioitinhISearchLookupEdit.EditValue = editor.Properties.View.GetRowCellValue(rowhandle, "gioitinh")
        Me.NgaythangnamsinhIDateEdit.EditValue = editor.Properties.View.GetRowCellValue(rowhandle, "ngaythangnamsinh")
        Me.NamsinhITextEdit.EditValue = editor.Properties.View.GetRowCellValue(rowhandle, "namsinh")
        Me.SobhytITextEdit.EditValue = editor.Properties.View.GetRowCellValue(rowhandle, "sobhyt")
        Me.NgaybatdauIDateEdit.EditValue = editor.Properties.View.GetRowCellValue(rowhandle, "ngaybatdau")
        Me.NgayhethanIDateEdit.EditValue = editor.Properties.View.GetRowCellValue(rowhandle, "ngayhethan")
        Me.TraituyenICheckEdit.EditValue = editor.Properties.View.GetRowCellValue(rowhandle, "traituyen")
        Me.NghenghiepISearchLookupEdit.EditValue = editor.Properties.View.GetRowCellValue(rowhandle, "nghenghiep")
        Me.DiachiITextEdit.EditValue = editor.Properties.View.GetRowCellValue(rowhandle, "diachi")

    End Sub
    'Tính số lần đến và số lần nằm viện
    Private Sub MabenhnhanISearchLookupEdit_EditValueChanged(sender As Object, e As System.EventArgs) Handles MabenhnhanISearchLookupEdit.EditValueChanged
        Try
            If Me.getState = 1 And Me.getKiemTraMaTonTaiGrid(Me.MabenhnhanISearchLookupEdit.EditValue.ToString) Then
                If Not IsDBNull(Me.IGridViewDs01.GetRowCellValue(indexGrid, "solanden") AndAlso Not Me.IGridViewDs01.GetRowCellValue(indexGrid, "solanden")).ToString = "" AndAlso Not IsDBNull(Me.IGridViewDs01.GetRowCellValue(indexGrid, "solannamvien") AndAlso Not Me.IGridViewDs01.GetRowCellValue(indexGrid, "solannamvien")).ToString = "" Then
                    Me.SolandenITextEdit.EditValue = CInt(Me.IGridViewDs01.GetRowCellValue(indexGrid, "solanden")) + 1
                    Me.SolannamvienITextEdit.EditValue = CInt(Me.IGridViewDs01.GetRowCellValue(indexGrid, "solannamvien")) + 1
                    Me.NgaykhamgannhatIDateEdit.EditValue = Me.IGridViewDs01.GetRowCellValue(indexGrid, "ngaytiepnhan")
                End If
            Else
                Me.SolandenITextEdit.EditValue = 1
                Me.SolannamvienITextEdit.EditValue = 1
            End If
        Catch ex As Exception

        End Try

    End Sub
    'Kiểm tra bệnh nhân đã tồn tại
    Private Function getKiemTraMaTonTaiGrid(ByVal sMaBenhNhan As String) As Boolean 'kiểm tra sự tồn tại của mã - dùng kiểm tra chung với igridview
        Dim acc As Integer
        For index = 0 To Me.IGridViewDs01.RowCount - 2
            If Me.IGridViewDs01.GetRowCellValue(index, "mabenhnhan").ToString.ToUpper = sMaBenhNhan.ToUpper Then
                acc = acc + 1
                Me.indexGrid = index
            End If
        Next
        If acc > 0 Then
            Return True
        End If
        Return False
    End Function
    'Hiện group BHYT khi loại bệnh nhân là BHYT
    Private Sub LoaibenhnhanISearchLookupEdit_EditValueChanged(sender As Object, e As System.EventArgs) Handles LoaibenhnhanISearchLookupEdit.EditValueChanged
        If Me.getState <> 0 And Not IsDBNull(Me.LoaibenhnhanISearchLookupEdit.EditValue) And Not IsNothing(Me.LoaibenhnhanISearchLookupEdit.EditValue) Then
            If Me.LoaibenhnhanISearchLookupEdit.EditValue.ToString = "BHYT" Then
                Me.LayoutControlGroupBHYT.Enabled = True
                Me.SobhytITextEdit.Properties.ReadOnly = False
                Me.NgaybatdauIDateEdit.Properties.ReadOnly = False
                Me.NgayhethanIDateEdit.Properties.ReadOnly = False
                Me.TraituyenICheckEdit.Properties.ReadOnly = False
            Else
                Me.LayoutControlGroupBHYT.Enabled = False
                Me.SobhytITextEdit.Properties.ReadOnly = True
                Me.NgaybatdauIDateEdit.Properties.ReadOnly = True
                Me.NgayhethanIDateEdit.Properties.ReadOnly = True
                Me.TraituyenICheckEdit.Properties.ReadOnly = True
            End If
        End If
    End Sub
    'Kiểm tra lúc state =2 Hiện group BHYT khi loại bệnh nhân là BHYT
    Public Overrides Sub setSubSau_Sua()
        If Not IsDBNull(Me.LoaibenhnhanISearchLookupEdit.EditValue) Then
            If Me.LoaibenhnhanISearchLookupEdit.EditValue.ToString = "BHYT" And Not IsNothing(Me.LoaibenhnhanISearchLookupEdit.EditValue) Then
                Me.LayoutControlGroupBHYT.Enabled = True
                Me.SobhytITextEdit.Properties.ReadOnly = False
                Me.NgaybatdauIDateEdit.Properties.ReadOnly = False
                Me.NgayhethanIDateEdit.Properties.ReadOnly = False
                Me.TraituyenICheckEdit.Properties.ReadOnly = False
            Else
                Me.LayoutControlGroupBHYT.Enabled = False
                Me.SobhytITextEdit.Properties.ReadOnly = True
                Me.NgaybatdauIDateEdit.Properties.ReadOnly = True
                Me.NgayhethanIDateEdit.Properties.ReadOnly = True
                Me.TraituyenICheckEdit.Properties.ReadOnly = True
            End If
        End If

    End Sub

    Private Sub ChọnbToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles ChọnbToolStripMenuItem.Click
        Me.LoaibenhnhanISearchLookupEdit.EditValue = Me.IGridViewDsThamChieu1.GetFocusedRowCellValue("loai")
        Me.MabenhnhanISearchLookupEdit.EditValue = Me.IGridViewDsThamChieu1.GetFocusedRowCellValue("ma")
        Me.TenITextEdit.EditValue = Me.IGridViewDsThamChieu1.GetFocusedRowCellValue("ten")
        Me.DantocISearchLookupEdit.EditValue = Me.IGridViewDsThamChieu1.GetFocusedRowCellValue("dantoc")
        Me.GioitinhISearchLookupEdit.EditValue = Me.IGridViewDsThamChieu1.GetFocusedRowCellValue("gioitinh")
        Me.NgaythangnamsinhIDateEdit.EditValue = Me.IGridViewDsThamChieu1.GetFocusedRowCellValue("ngaythangnamsinh")
        Me.NamsinhITextEdit.EditValue = Me.IGridViewDsThamChieu1.GetFocusedRowCellValue("namsinh")
        Me.SobhytITextEdit.EditValue = Me.IGridViewDsThamChieu1.GetFocusedRowCellValue("sobhyt")
        Me.NgaybatdauIDateEdit.EditValue = Me.IGridViewDsThamChieu1.GetFocusedRowCellValue("ngaybatdau")
        Me.NgayhethanIDateEdit.EditValue = Me.IGridViewDsThamChieu1.GetFocusedRowCellValue("ngayhethan")
        Me.TraituyenICheckEdit.EditValue = Me.IGridViewDsThamChieu1.GetFocusedRowCellValue("traituyen")
        Me.NghenghiepISearchLookupEdit.EditValue = Me.IGridViewDsThamChieu1.GetFocusedRowCellValue("nghenghiep")
        Me.DiachiITextEdit.EditValue = Me.IGridViewDsThamChieu1.GetFocusedRowCellValue("diachi")

    End Sub

    Private Sub BarButtonItemLuuPopUp2_ItemClick(sender As System.Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItemLuuPopUp2.ItemClick
        Me.IGridViewPopUp2.setUpdateData()
        For index = 0 To Me.IGridViewPopUp2.RowCount - 1
            If CBool(Me.IGridViewPopUp2.GetRowCellValue(index, "Mark")) = True AndAlso Me.getKiemTraMaTonTaiIGridViewChitiet01(Me.IGridViewPopUp2.GetRowCellValue(index, "ma")) Then
                Me.IGridViewChiTiet01.SetFocusedRowCellValue("madichvu", Me.IGridViewPopUp2.GetRowCellValue(index, "ma").ToString)
                Me.IGridViewChiTiet01.SetFocusedRowCellValue("tendichvu", Me.IGridViewPopUp2.GetRowCellValue(index, "ten").ToString)
                Me.IGridViewChiTiet01.SetFocusedRowCellValue("dongia", CDec(Me.IGridViewPopUp2.GetRowCellValue(index, "dongia")))
                Me.IGridViewChiTiet01.SetFocusedRowCellValue("noithuchien", Me.IGridViewPopUp2.GetRowCellValue(index, "noithuchien").ToString)
                Me.IGridViewChiTiet01.SetFocusedRowCellValue("soluong", 1)
                Me.IGridViewChiTiet01.AddNewRow()
            End If
            Me.IGridViewPopUp2.SetRowCellValue(index, "Mark", False)
        Next
        ' Delete row cuối cùng bị dư
        If IsDBNull(Me.IGridViewChiTiet01.GetFocusedRowCellValue("madichvu")) Then
            Me.IGridViewChiTiet01.DeleteSelectedRows()
        End If
        'Duyệt trong grid có dòng dư 
        For index = 0 To Me.IGridViewChiTiet01.RowCount - 1
            If IsDBNull(Me.IGridViewChiTiet01.GetRowCellValue(index, "madichvu")) Then
                Me.IGridViewChiTiet01.DeleteRow(index)
            End If
        Next

    End Sub
    'Kiểm tra sự tồn tại của madichvu trong IGridViewChitiet01
    Private Function getKiemTraMaTonTaiIGridViewChitiet01(ByVal sMadichvu As String) As Boolean
        Dim acc As Integer
        For index = 0 To Me.IGridViewChiTiet01.RowCount - 2
            If Me.IGridViewChiTiet01.GetRowCellValue(index, "madichvu").ToString.ToUpper = sMadichvu.ToUpper Then
                acc = acc + 1
            End If
        Next
        If acc > 0 Then
            Return False
        End If
        Return True
    End Function

    Private Sub BarSubItemPhieuChiDinh_ItemClick(sender As Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarSubItemPhieuChiDinh.ItemClick
        Dim report As Report_PhieuChiDinh_TiepNhanBN = New Report_PhieuChiDinh_TiepNhanBN
        report.TiepNhanBenhNhanTableAdapter1.FillMa_TiepNhanBenhNhan(report.PcV_CLINICDataSet1.TiepNhanBenhNhan, Me.IGridViewDs01.GetFocusedRowCellValue("ma"))
        report.TiepNhanBenhNhanDichVuTableAdapter1.FillMa_TiepNhanBenhNhanDichVu(report.PcV_CLINICDataSet1.TiepNhanBenhNhanDichVu, Me.IGridViewDs01.GetFocusedRowCellValue("ma"))
        report.NgayThangNam.Value = "Ngày " & Format(CDate(Me.NgaytiepnhanIDateEdit.EditValue), "dd") & " Tháng " & Format(CDate(Me.NgaytiepnhanIDateEdit.EditValue), "MM") & " Năm " & Format(CDate(Me.NgaytiepnhanIDateEdit.EditValue), "yyyy")
        report.ShowRibbonPreview()

    End Sub

    Private Sub BarSubItemPhieuTiepNhan_ItemClick(sender As Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarSubItemPhieuTiepNhan.ItemClick
        Dim report As Report_TiepNhanBenhNhan = New Report_TiepNhanBenhNhan
        report.TiepNhanBenhNhanTableAdapter1.FillMa_TiepNhanBenhNhan(report.PcV_CLINICDataSet1.TiepNhanBenhNhan, Me.IGridViewDs01.GetFocusedRowCellValue("ma"))
        report.NgayThangNam.Value = "Ngày " & Format(CDate(Me.NgaytiepnhanIDateEdit.EditValue), "dd") & " Tháng " & Format(CDate(Me.NgaytiepnhanIDateEdit.EditValue), "MM") & " Năm " & Format(CDate(Me.NgaytiepnhanIDateEdit.EditValue), "yyyy")
        report.ShowRibbonPreview()
    End Sub
End Class
